package com.example.yingjiguanli_demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.yingjiguanli_demo.pojo.Questions;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author scj
 * @since 2024-11-04
 */
@Mapper
public interface QuestionsMapper extends BaseMapper<Questions> {
    /**
     * 插入题目
     *
     * @param question *
     */
    @Insert("INSERT INTO questions (question_text, question_type, is_multiple_choice, correct_answer, subject_id) " +
            "VALUES (#{questionText}, #{questionType}, #{isMultipleChoice}, #{correctAnswer}, #{subjectId})")
    @Options(useGeneratedKeys = true, keyProperty = "questionId")
    void questionsInsert(Questions question);


    /**
     * 根据科目id查询随机题目
     * @param subjectId 科目id
     * @param limit 题目数量
     * @return List<Questions>
     */
    @Select("SELECT * FROM questions WHERE subject_id = #{subjectId} ORDER BY RAND() LIMIT #{limit}")
    List<Questions> getRandomQuestionsBySubject(@Param("subjectId") int subjectId, @Param("limit") int limit);


    /**
     * 根据id查询题目
     * @param questionId 题目id
     * @return Questions
     */
    @Select("SELECT question_id, question_text, question_type, is_multiple_choice, created_at, updated_at, correct_answer, subject_id FROM questions WHERE question_id = #{questionId}")
    Questions selectById(@Param("questionId") int questionId);

    /**
     * 根据题目内容查询
     * @param questionText 题目内容
     * @return Questions
     */
    @Select("SELECT * FROM questions WHERE question_text = #{questionText} LIMIT 1")
    Questions findByQuestionText(String questionText);

}
